package com.wutuo.wtservice.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.util.CommonUtil;
import com.wutuo.wtservice.api.ServiceAPIService;
import com.wutuo.wtservice.api.request.ListServiceRequest;
import com.wutuo.wtservice.api.request.ServiceRequest;
import com.wutuo.wtservice.api.request.WtTypeRequest;
import com.wutuo.wtservice.api.response.ServiceEnumResponse;
import com.wutuo.wtservice.api.response.ServiceResponse;
import com.wutuo.wtservice.api.response.WtTypeResponse;
import com.wutuo.wtservice.provider.component.ServiceDataConvert;
import com.wutuo.wtservice.provider.dao.ServiceMapper;
import com.wutuo.wtservice.provider.domain.WtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 服务表 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service public class ServiceService extends BaseService<WtService, Long>
    implements ServiceAPIService {

    private Logger logger = LoggerFactory.getLogger(ServiceService.class);

    @Autowired private ServiceMapper serviceMapper;

    @Autowired private WtTypeService wtTypeService;

    @Override protected GenericMapper<WtService, Long> getMapper() {

        // TODO Auto-generated method stub
        return serviceMapper;
    }

    public void addorUpdate(ServiceRequest record) {

        if (record.getId() == null) {
            super.add(record);
        } else {
            super.update(record);
        }
    }

    public void del(Long id) {

        ServiceRequest model = new ServiceRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<ServiceResponse> lisForPage(ListServiceRequest request) {

        List<ServiceResponse> list = serviceMapper.lisForPage(request);
        ServiceDataConvert.convertToServiceResponseList(list);
        Collections.sort(list, new Comparator<ServiceResponse>() {

            @Override public int compare(ServiceResponse a, ServiceResponse b) {

                if (a.getSort() != null && b.getSort() != null) {
                    return a.getSort().compareTo(b.getSort());
                }
                return -1;
            }
        });
        return list;
    }

    public List<ServiceResponse> list(ServiceRequest request) {

        request.setDelFlag(true);
        List<ServiceResponse> list = serviceMapper.list(request);
        ServiceDataConvert.convertToServiceResponseList(list);
        Collections.sort(list, new Comparator<ServiceResponse>() {

            @Override public int compare(ServiceResponse a, ServiceResponse b) {

                if (a.getSort() != null && b.getSort() != null) {
                    return a.getSort().compareTo(b.getSort());
                }
                return -1;
            }
        });

        return list;
    }

    /**
     * 获取服务信息
     *
     * @param <T>
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public <T> void getUseServiceByIds(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        StringBuffer idIn = CommonUtil.getLongInt(list, tKey);
        ServiceRequest request = new ServiceRequest();
        request.setIds(idIn.toString());
        List<ServiceResponse> listUser = list(request);
        List<ServiceEnumResponse> listWtService = createServiceEnumResponse(listUser);
        Map map = CommonUtil.listToObject(listWtService, "id");
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            String[] ids = id.split(",");
            List<T> s = new ArrayList();
            for (int j = 0; j < ids.length; j++) {
                if (map.containsKey(ids[j])) {
                    if (!s.contains(map.get(ids[j]))) {
                        s.add((T) map.get(ids[j]));
                    }
                }
            }
            CommonUtil.setProperty(modelS, skey, s);
        }
    }

    public List<ServiceEnumResponse> createServiceEnumResponse(List<ServiceResponse> list) {

        List<ServiceEnumResponse> listResult = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            ServiceResponse m = list.get(i);
            ServiceEnumResponse s = new ServiceEnumResponse();
            s.setId(m.getId());
            s.setName(m.getName());
            s.setTypeImage(m.getTypeImage());
            listResult.add(s);
        }

        return listResult;
    }

    /**
     * 获取服务信息
     *
     * @param <T>
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public <T> void getUseServiceByInstationIds(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        StringBuffer idIn = CommonUtil.getLongInt(list, tKey);
        ServiceRequest request = new ServiceRequest();
        request.setCmInstitutionIds(idIn.toString());
        List<ServiceResponse> listUser = list(request);
        Map map = CommonUtil.listToObjectEqual(listUser, "cmInstitutionId");
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            if (map.containsKey(id)) {
                CommonUtil.setProperty(modelS, skey, map.get(id));
            }

        }
    }

    public void initService(Long id) {

        List<WtTypeResponse> list = wtTypeService.list(new WtTypeRequest());
        for (int i = 0; i < list.size(); i++) {
            WtTypeResponse p = list.get(i);
            ServiceRequest s = new ServiceRequest();
            s.setCmInstitutionId(id);
            s.setType(p.getId());
            s.setName(p.getName());
            addorUpdate(s);
        }
    }
}